﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MasterPages_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PreRender += new EventHandler(InitPage);
        }
    }

    protected void InitPage(object sender, EventArgs e)
    {
        InitData();
    }
    protected void InitData()
    {
        //String sqlLuckDistrict = "SELECT delivery.id as delivery_id,code,name,phone,delivery_date from courier,delivery " +
        //                        " where finished=0 and courier_id=courier.id ORDER BY status asc";

        //DataSet ds_luckydistrict = DB.QuerySelect(sqlLuckDistrict);

        //delivery_datagrid.DataSource = ds_luckydistrict;
        //delivery_datagrid.DataBind();
    }
    protected void OnClickDetailBtn(object sender, DataGridCommandEventArgs e)
    {
        //// Get current delivery detail info
        //String delivery_id = delivery_datagrid.DataKeys[e.Item.ItemIndex].ToString();
        //Button dbtn = (Button)delivery_datagrid.Items[e.Item.ItemIndex].FindControl("detail_button");
        //if (dbtn.Text.Equals("回收盈余"))
        //{
        //    String info = "";
        //    float get = 0;
        //    float pay = 0;
        //    float extra = 0;
        //    // get all it's left result
        //    String sqlAllLeftDeliveryId = "SELECT id as delivery_id from delivery inner join "+
        //                                "(SELECT courier_id from delivery where id="+delivery_id+") S on S.courier_id=delivery.courier_id "+
        //                          "WHERE returned=0";
        //    DataSet ds_leftdeliveryid = DB.QuerySelect(sqlAllLeftDeliveryId);

        //    for (int i = 0; i < ds_leftdeliveryid.Tables[0].Rows.Count; i++)
        //    {
        //        String[] reporti = DB.GetDeliveryReport(ds_leftdeliveryid.Tables[0].Rows[i].ItemArray[0].ToString());

        //        get += float.Parse(reporti[0]);
        //        pay += float.Parse(reporti[1]);
        //        extra += float.Parse(reporti[2]);

        //        info += "收回" + reporti[0] + "\t" + "付出" + reporti[1] + "余" + reporti[2] + "\n";

        //        // set it returned
        //        String sqlSetReturned = "UPDATE delivery set returned=1 where id="+
        //            ds_leftdeliveryid.Tables[0].Rows[i].ItemArray[0].ToString();
        //        DB.QueryAlter(sqlSetReturned);
        //    }

        //    info += "总计:\n" + "收回" + get + "\t" + "付出" + pay + "余" + extra;

        //    detail_delivery_textbox.Text = info;

        //    dbtn.Enabled = false;
        //}

        //dbtn.Text = "回收盈余";

        //String[] report = DB.GetDeliveryReport(delivery_id);

        //detail_delivery_textbox.Text = report[5];

        //// update other free man
        //String sqlUpgradeFreeMen = "UPDATE courier set status=status+1 where available=1 and status>0";
        //DB.QueryAlter(sqlUpgradeFreeMen);

        //// free this courier
        //String sqlFreeCourier = "UPDATE courier set status=1 from courier,delivery where delivery.id="+delivery_id+" and courier_id=courier.id";
        //DB.QueryAlter(sqlFreeCourier);

        //// finish this delivery
        //String sqlFinishDispatch = "UPDATE delivery set finished=1 where id="+delivery_id;
        //DB.QueryAlter(sqlFinishDispatch);

        //// finish the orders of the delivery
        //String sqlFinishOrders = "UPDATE orders set status=status+1 where delivery_id="+delivery_id;
        //DB.QueryAlter(sqlFinishOrders);
        
    }
}
